/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author xubuntu
 */
@Entity
@Table(name = "Event")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findById", query = "SELECT e FROM Event e WHERE e.id = :id"),
    @NamedQuery(name = "Event.findByName", query = "SELECT e FROM Event e WHERE e.name = :name"),
    @NamedQuery(name = "Event.findByPlace", query = "SELECT e FROM Event e WHERE e.place = :place"),
    @NamedQuery(name = "Event.findByDateStart", query = "SELECT e FROM Event e WHERE e.dateStart = :dateStart"),
    @NamedQuery(name = "Event.findByDateEnd", query = "SELECT e FROM Event e WHERE e.dateEnd = :dateEnd"),
    @NamedQuery(name = "Event.findByTypeIo", query = "SELECT e FROM Event e WHERE e.typeIo = :typeIo"),
    @NamedQuery(name = "Event.findByTypePp", query = "SELECT e FROM Event e WHERE e.typePp = :typePp"),
    @NamedQuery(name = "Event.findByDescription", query = "SELECT e FROM Event e WHERE e.description = :description"),
    @NamedQuery(name = "Event.findByCreationDate", query = "SELECT e FROM Event e WHERE e.creationDate = :creationDate"),
    @NamedQuery(name="Event.findAllEventOrganizedUser", query="SELECT e FROM Event e WHERE e.organizer = :organizer"),
    
})
public class Event implements Serializable,Comparable {
    private static final long serialVersionUID = 1L;
    public static final String FIND_ALL = "Event.findAll";
    public static final String FIND_ALL_EVENT_ORGANIZED = "Event.findAllEventOrganizedUser";
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "place")
    private String place;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date_Start")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateStart;
    @Column(name = "date_End")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateEnd;
    @Basic(optional = false)
    @NotNull
    @Column(name = "type_io")
    private int typeIo;
    @Basic(optional = false)
    @NotNull
    @Column(name = "type_pp")
    private int typePp;
    @Size(max = 45)
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "creationDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event")
    private Collection<Invitation> invitationCollection;
    @JoinColumn(name = "organizer", referencedColumnName = "nickname")
    @ManyToOne(optional = false)
    private User organizer;
    @Column(name = "firstDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date firstDate;
    
    public Event() {
    }

    public Event(Integer id) {
        this.id = id;
    }

    public Event( String name, String place, Date dateStart, Date dateEnd, int typeIo, int typePp,String description, User organizer) {
       
        this.name = name;
        this.place = place;
        this.dateStart = dateStart;
        this.typeIo = typeIo;
        this.typePp = typePp;
        this.dateEnd = dateEnd;
        this.description = description;
        this.creationDate = new Date();
        this.organizer = organizer;
        this.firstDate = new Date();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public Date getFirstDate() {
        return firstDate;
    }

    public void setFirstDate(Date firstDate) {
        this.firstDate = firstDate;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public int getTypeIo() {
        return typeIo;
    }

    public void setTypeIo(int typeIo) {
        this.typeIo = typeIo;
    }

    public int getTypePp() {
        return typePp;
    }

    public void setTypePp(int typePp) {
        this.typePp = typePp;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @XmlTransient
    public Collection<Invitation> getInvitationCollection() {
        return invitationCollection;
    }

    public void setInvitationCollection(Collection<Invitation> invitationCollection) {
        this.invitationCollection = invitationCollection;
    }

    public User getOrganizer() {
        return organizer;
    }

    public void setOrganizer(User organizer) {
        this.organizer = organizer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Event[ id=" + id + " ]";
    }

    @Override
    public int compareTo(Object o) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
       if( !this.dateStart.before( new Date() ))
       {    
            if(this.dateStart.after(((Event)o).dateStart))
                return 1;
            return -1;
       }
       else
       {
            if(this.dateStart.after(((Event)o).dateStart))
                return -1;
            return 1;
       } 
    }
    
}
